package Methode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Methode {

	protected Shell shell;
	private StyledText getSetters;
	private String setAndGet = "";
	private String classBuilder = "public class ";
	private String constructBuilder = "    public ";
	private String packageName = "";
	private String vars = "";

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Methode window = new Methode();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(679, 474);
		shell.setText("Methodes!");
		String nL = System.getProperty("line.separator");
		final StyledText dataType = new StyledText(shell, SWT.BORDER);
		dataType.setText("SaleCtr" + nL + "Package::CtrLayer" + nL + "--" + nL + "- sale: Sale" + nL + "--" + nL + "+ createSale() : Sale" + nL +
				"+ addItem(int id, int amount);" + nL + "+ addItem(String name, int amount);" + nL + "+ finishSale(int employeeNr);" + nL + 
				"+ setCustomer(Customer c);");
		dataType.setBounds(10, 21, 231, 373);
		
		getSetters = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL);
		getSetters.setBounds(247, 21, 406, 373);
		
		Button btnGo = new Button(shell, SWT.NONE);
		btnGo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String full = "";
				int line = 0;
				int linecount = dataType.getLineCount();
				String nL = System.getProperty("line.separator");
				
				for(int i=0; i<linecount; i++){
					
					String test = dataType.getLine(line);
					String meth = "";
					if(!test.equals("--")){
						if(i == 0){
							meth = test;
							classBuilder += meth + "{";
							constructBuilder += meth + " {" + nL + nL + "    }";
						}else if(i == 1){
							meth = test.substring(test.indexOf("::")+2, test.length());
							packageName = "package " + meth + ";" + nL;
						}else{
							if(test.contains("(")){
								meth = writeMethod(test);
								full += meth + nL + nL;
							}else if(test.contains(":")){
								meth = writeSimpleVar(test);
								vars += meth + nL;
								setAndGet += writeSetAndGet(meth);
							}
						}
					}
					line++;
				}
				
				full += setAndGet + nL + "}";
				
				String show = packageName + nL + classBuilder + nL + vars + nL + constructBuilder + nL + nL + full;
				
				getSetters.setText(show);
			}

		});
		btnGo.setBounds(88, 400, 75, 25);
		btnGo.setText("Go");
		
		

	}
	
	private String writeSetAndGet(String line){
		String words[] = line.split(" ");
		String setGet = "";
		String nL = System.getProperty("line.separator");
		words[2] = words[2].substring(0, words[2].length() - 1);
		
		String upper = words[2];
		
		char firstupper = Character.toUpperCase(upper.charAt(0));
		
		upper = firstupper + upper.substring(1);
		
		String comment1 = "    /**" + nL;
		String comment2 = "    * get" + upper + " - Returns the " + upper + "\n	* @return " + words[2] + nL;
		String comment3 = "    */" + nL;
		String word1 = "	public " + words[1] + " get" + upper + "(){" + nL;
		String word2 = "		return " + words[2] + ";" + nL;
		String word3 = "	}" + nL;
		
		String comment4 = "    * set" + upper + " - sets the " + upper + nL + "	* @param " + words[1] + " " + words[2] + nL;
		
		String word4 = "	public void set" + upper + "(" + words[1] + " " + words[2] + "){" + nL;
		String word5 = "		this." + words[2] + " = " + words[2] + ";" + nL;
		String word6 = "	}" + nL;
		
		setGet = comment1 + comment2 + comment3 + word1 + word2 + word3 + nL + comment1 + comment4 + comment3 + word4 + word5 + word6 + nL;
		
		
		return setGet;
	}
	
	private String writeSimpleVar(String test) {
		String nL = System.getProperty("line.separator");
		String words[] = test.split(" ");
		String meth = "";
		
		if(words[0].charAt(0) == '_'){
			if(words[0].charAt(1) == '+'){
				meth += "	public static ";
			}else if(words[0].charAt(1) == '-'){
				meth += "	private static ";
			}else if(words[0].charAt(1) == '#'){
				meth += "	protected static ";
			}
			meth += words[2] + " ";
			meth += words[1].replace(":", ";");
		} else {
			if(words[0].charAt(0) == '+'){
				meth += "	public ";
			}else if(words[0].charAt(0) == '-'){
				meth += "	private ";
			}else if(words[0].charAt(0) == '#'){
				meth += "	protected ";
			}
			meth += words[2] + " ";
			meth += words[1].replace(":", ";");
		}
		
		return meth;
	}
	
	private String writeMethod(String test){
		String nL = System.getProperty("line.separator");
		String param = "";
		
		String meth = "";
		if(test.indexOf("(") != test.indexOf(")")-1){
			String parameters = test.substring(test.indexOf("(")+1, test.indexOf(")"));
			String rest = test.substring(0, test.indexOf("(")+1) + test.substring(test.indexOf(")"), test.length());
			
			String words[] = rest.split(" ");
			words[1] = words[1].substring(0, words[1].indexOf("(")+1) + parameters + words[1].substring(words[1].indexOf(")"), words[1].length());
			if(words[0].charAt(0) == '_'){
				if(words[0].charAt(1) == '+'){
					meth += "	public static ";
				}else if(words[0].charAt(1) == '-'){
					meth += "	private static ";
				}
				
				if(words.length > 2 & words[2].equals(":")){
					String retType = words[3];
					if(retType.charAt(retType.length()-1) == '_'){
						retType = retType.substring(0, retType.length()-2);
					}
					meth += retType + " ";
				} else {
					meth += "void ";
				}
				
				meth += words[1] + "{" + nL + "			//TODO - Auto Generated" + nL + "	}";
			}else if(words.length > 2 & words[0].charAt(0) == '+' || words[0].charAt(0) == '-'){
				if(words[0].charAt(0) == '+'){
					meth += "	public ";
				}else if(words[0].charAt(0) == '-'){
					meth += "	private ";
				}
				
				String retType = words[3];
				if(retType.charAt(retType.length()-1) == '_'){
					retType = retType.substring(0, retType.length()-2);
				}
				meth += retType + " " + words[1] + "{" + nL + "			//TODO - Auto Generated" + nL + "	}";
				
			}else if(words[0].charAt(0) == '+' || words[0].charAt(0) == '-'){
				if(words[0].charAt(0) == '+'){
					meth += "	public ";
				}else if(words[0].charAt(0) == '-'){
					meth += "	private ";
				}
				String methName = words[1];
				if(methName.charAt(methName.length()-1) == ';'){
					methName = methName.substring(0, methName.length()-1);
				}

				meth += "void " + methName + "{" + nL + "		//TODO - Auto Generated" + nL + "	}";
			}
		} else {
			String words[] = test.split(" ");
			if(words[0].charAt(0) == '_'){
				if(words[0].charAt(1) == '+'){
					meth += "	public static ";
				}else if(words[0].charAt(1) == '-'){
					meth += "	private static ";
				}
				
				if(words.length > 2 & words[2].equals(":")){
					String retType = words[3];
					if(retType.charAt(retType.length()-1) == '_'){
						retType = retType.substring(0, retType.length()-2);
					}
					meth += retType + " ";
				} else {
					meth += "void ";
				}
				
				meth += words[1] + "{" + nL + "			//TODO - Auto Generated" + nL + "	}";
			}else if(words.length > 2 & words[0].charAt(0) == '+' || words[0].charAt(0) == '-'){
				if(words[0].charAt(0) == '+'){
					meth += "	public ";
				}else if(words[0].charAt(0) == '-'){
					meth += "	private ";
				}
				
				String retType = words[3];
				if(retType.charAt(retType.length()-1) == '_'){
					retType = retType.substring(0, retType.length()-2);
				}
				meth += retType + " " + words[1] + "{" + nL + "			//TODO - Auto Generated" + nL + "	}";
				
			}else if(words[0].charAt(0) == '+' || words[0].charAt(0) == '-'){
				if(words[0].charAt(0) == '+'){
					meth += "	public ";
				}else if(words[0].charAt(0) == '-'){
					meth += "	private ";
				}
				String methName = words[1];
				if(methName.charAt(methName.length()-1) == ';'){
					methName = methName.substring(0, methName.length()-1);
				}

				meth += "void " + methName + "{" + nL + "			//TODO - Auto Generated" + nL + "	}";
			}

		}
		return meth;
	}

}
